package com.lancoo.ccas53.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lancoo.ccas53.aspect.LogAnnotation;
import com.lancoo.ccas53.entity.Course;
import com.lancoo.ccas53.entity.RuleTimeProhibit;
import com.lancoo.ccas53.entity.RuleUsable;
import com.lancoo.ccas53.pojo.common.PageInfo;
import com.lancoo.ccas53.pojo.dto.SetCourseDto;
import com.lancoo.ccas53.pojo.vo.*;
import com.lancoo.ccas53.service.CourseService;
import com.lancoo.ccas53.service.RuleTimeProhibitService;
import com.lancoo.ccas53.service.RuleUsableService;
import com.lancoo.ccas53.util.ResponseObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: liufeng
 * @CreateTime: 2024-06-13  10:21
 * @Description:
 */
@Api(tags = "排课规则")
@RestController
@RequestMapping("/rule")
public class RuleController {

    @Resource
    private RuleUsableService ruleUsableService;
    @Resource
    private RuleTimeProhibitService ruleTimeProhibitService;
    @Resource
    private CourseService courseService;

    @GetMapping("/getBasicRule")
    @ApiOperation("获取基础规则")
    public ResponseObject<List<RuleUsable>> getBasicRule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        return ResponseObject.ofSuccess(ruleUsableService.getBasicRule(taskId));
    }

    @PostMapping("/setBasicRule")
    @ApiOperation("设置基础规则")
    @LogAnnotation(operationType = "2", operationContent = "设置基础规则")
    public ResponseObject<List<RuleUsable>> setBasicRule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                         @ApiParam(value = "基础规则", required = true) @Validated @RequestBody List<RuleUsable> ruleUsableList) {
        ruleUsableService.setBasicRule(taskId, ruleUsableList);
        return ResponseObject.ofSuccess(ruleUsableService.getBasicRule(taskId));
    }

    @GetMapping("/getProhibit")
    @ApiOperation("获取禁排")
    public ResponseObject<List<RuleTimeProhibit>> getTimeProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                  @ApiParam(value = "1、日期禁排 2、场地禁排 3、教学班课程禁排优先排 4、教师禁排优先排", required = true) @RequestParam Integer optionType,
                                                                  @ApiParam(value = "教室id/教学班id/教师id ", required = false) @RequestParam(required = false) Long optionId,
                                                                  @ApiParam(value = "如果是教学班 传授课方式", required = false) @RequestParam(required = false) Integer hourType) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getTimeProhibit(taskId, optionType, optionId, hourType));
    }

    @PostMapping("/setProhibit")
    @ApiOperation("设置禁排")
    @LogAnnotation(operationType = "2", operationContent = "设置禁排")
    public ResponseObject<Boolean> setTimeProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                   @ApiParam(value = "1、日期禁排 2、场地禁排 4、教师禁排优先排", required = true) @RequestParam Integer optionType,
                                                   @ApiParam(value = "教室id/教师id ", required = false) @RequestParam(required = false) String optionIds,
                                                   @ApiParam(value = "日期禁排实体类 ", required = true) @Validated @RequestBody(required = false) List<RuleTimeProhibit> ruleTimeProhibits) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.setTimeProhibit(taskId, optionType, optionIds, ruleTimeProhibits));
    }

    @PostMapping("/clearProhibit")
    @ApiOperation("清空规则")
    @LogAnnotation(operationType = "4", operationContent = "清空规则")
    public ResponseObject<Boolean> clearProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                 @ApiParam(value = "1、日期禁排 2、场地禁排  4、教师禁排优先排", required = true) @RequestParam Integer optionType,
                                                 @ApiParam(value = "教室id/教师id ", required = false) @RequestBody(required = false) List<Long> optionIds,
                                                 @ApiParam(value = "清空", required = true) @RequestParam boolean all) {
        ruleTimeProhibitService.clearProhibit(taskId, optionType, optionIds, all);
        return ResponseObject.ofSuccess();
    }

    @GetMapping("/getRoomListWithRule")
    @ApiOperation("获取所有教室和规则")
    @LogAnnotation(operationType = "3", operationContent = "获取所有教室和规则")
    public ResponseObject<PageInfo<RoomWithRuleVo>> getRoomListWithRule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                        @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId,
                                                                        @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                                        @ApiParam(value = "楼层", required = false) @RequestParam(required = false) Integer floor,
                                                                        @ApiParam(value = "教室类型", required = false) @RequestParam(required = false) Integer roomType,
                                                                        @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                                        @ApiParam(value = "学院", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                        @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置") @RequestParam Integer dataRange,
                                                                        @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                        @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getRoomListWithRule(taskId, campusId, buildingId, floor, roomType, keyword, baseCollegeId, dataRange, currentPage, pageSize));
    }

    @GetMapping("/getRoomRuleStatic")
    @ApiOperation("获取教室规则统计信息")
    @LogAnnotation(operationType = "3", operationContent = "获取教室规则统计信息")
    public ResponseObject<RuleStatic> getRoomRuleStatic(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                        @ApiParam(value = "校区id", required = false) @RequestParam(required = false) Integer campusId,
                                                        @ApiParam(value = "楼宇id", required = false) @RequestParam(required = false) Integer buildingId,
                                                        @ApiParam(value = "楼层", required = false) @RequestParam(required = false) Integer floor,
                                                        @ApiParam(value = "教室类型", required = false) @RequestParam(required = false) Integer roomType,
                                                        @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                        @ApiParam(value = "学院", required = false) @RequestParam(required = false) String baseCollegeId,
                                                        @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置") @RequestParam Integer dataRange,
                                                        @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                        @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getRoomRuleStatic(taskId, campusId, buildingId, floor, roomType, keyword, baseCollegeId, dataRange, currentPage, pageSize));
    }

    @GetMapping("/getTeachingClassListWithRule")
    @ApiOperation("获取所有教学班和规则")
    @LogAnnotation(operationType = "3", operationContent = "获取所有教学班和规则")
    public ResponseObject<PageInfo<TeachingClassWithRuleVo>> getTeachingClassListWithRule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                                          @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                                          @ApiParam(value = "课程类别", required = false) @RequestParam(required = false) String courseTypeId,
                                                                                          @ApiParam(value = "课程性质", required = false) @RequestParam(required = false) Integer courseNature,
                                                                                          @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                                                          @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置", required = true) @RequestParam Integer dataRange,
                                                                                          @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                                          @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getTeachingClassListWithRule(taskId, baseCollegeId, courseTypeId, courseNature, keyword, dataRange, currentPage, pageSize));
    }

    @GetMapping("/getTeachingClassRuleStatic")
    @ApiOperation("获取教学班规则统计信息")
    @LogAnnotation(operationType = "3", operationContent = "获取教学班规则统计信息")
    public ResponseObject<RuleStatic> getTeachingClassRuleStatic(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                 @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                 @ApiParam(value = "课程类别", required = false) @RequestParam(required = false) String courseTypeId,
                                                                 @ApiParam(value = "课程性质", required = false) @RequestParam(required = false) Integer courseNature,
                                                                 @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                                 @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置", required = true) @RequestParam Integer dataRange,
                                                                 @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                 @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getTeachingClassRuleStatic(taskId, baseCollegeId, courseTypeId, courseNature, keyword, dataRange, currentPage, pageSize));
    }

    @PostMapping("/setTeachingClassProhibit")
    @ApiOperation("设置教学班禁排")
    @LogAnnotation(operationType = "2", operationContent = "设置教学班禁排")
    public ResponseObject<Boolean> setTeachingClassProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                            @ApiParam(value = "3、教学班禁排", required = true) @RequestParam Integer optionType,
                                                            @ApiParam(value = "教学班 日期禁排实体类 ", required = true) @Validated @RequestBody TeachingClassProhibitSetVo vo) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.setTeachingClassProhibit(taskId, optionType, vo.getTeachingClassWithRuleVos(), vo.getRuleTimeProhibits()));
    }

    @PostMapping("/clearTeachingClassProhibit")
    @ApiOperation("清空教学班禁排")
    @LogAnnotation(operationType = "4", operationContent = "清空教学班禁排")
    public ResponseObject<Boolean> clearTeachingClassProhibit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                              @ApiParam(value = "3、教学班禁排", required = true) @RequestParam Integer optionType,
                                                              @ApiParam(value = "教学班信息", required = true) @RequestBody List<TeachingClassWithRuleVo> teachingClassWithRuleVos) {
        ruleTimeProhibitService.clearTeachingClassProhibit(taskId, optionType, teachingClassWithRuleVos);
        return ResponseObject.ofSuccess();
    }

    @GetMapping("/getTeacherListWithRule")
    @ApiOperation("获取所有教师和规则")
    @LogAnnotation(operationType = "3", operationContent = "获取所有教师和规则")
    public ResponseObject<PageInfo<TeacherWithRuleVo>> getTeacherListWithRule(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                                              @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                                              @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                                              @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置") @RequestParam Integer dataRange,
                                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getTeacherListWithRule(taskId, baseCollegeId, keyword, dataRange, currentPage, pageSize));
    }

    @GetMapping("/getTeacherRuleStatic")
    @ApiOperation("获取教师规则统计信息")
    @LogAnnotation(operationType = "3", operationContent = "获取教师规则统计信息")
    public ResponseObject<RuleStatic> getTeacherRuleStatic(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                           @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId,
                                                           @ApiParam(value = "关键字", required = false) @RequestParam(required = false) String keyword,
                                                           @ApiParam(value = "数据范围 0：全部，1：已设置，2未设置") @RequestParam Integer dataRange,
                                                           @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                           @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        return ResponseObject.ofSuccess(ruleTimeProhibitService.getTeacherRuleStatic(taskId, baseCollegeId, keyword, dataRange, currentPage, pageSize));
    }

    @GetMapping("/getCourseLimit")
    @ApiOperation("获取所有课程限制")
    @LogAnnotation(operationType = "3", operationContent = "获取所有课程限制")
    public ResponseObject<SetCourseDto<Course>> getCourseList(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                              @ApiParam(value = "0全部 1已设置 2未设置") @RequestParam(required = false) Integer dataRange,
                                                              @ApiParam(value = "基础平台学院id") @RequestParam(required = false) String baseCollegeId,
                                                              @ApiParam(value = "课程类别id") @RequestParam(required = false) String courseTypeId,
                                                              @ApiParam(value = "课程性质 1必修 2选修") @RequestParam(required = false) Integer courseNature,
                                                              @ApiParam(value = "1、课程 2、环节") @RequestParam(required = false) Integer flag,
                                                              @ApiParam(value = "课程名模糊查") @RequestParam(required = false) String keyword,
                                                              @ApiParam(value = "参数是否设置  0、未设置  1、已设置") @RequestParam(required = false) Integer parameterSet,
                                                              @ApiParam(value = "页码", required = true) @RequestParam Integer currentPage,
                                                              @ApiParam(value = "页容", required = true) @RequestParam Integer pageSize) {
        Page<Course> page = new Page<>(currentPage, pageSize);
        return ResponseObject.ofSuccess(courseService.getCourseList(page, taskId, dataRange, baseCollegeId, courseTypeId, courseNature, flag, keyword, parameterSet));
    }

    @PostMapping("/setCourseLimit")
    @ApiOperation("设置课程限制")
    @LogAnnotation(operationType = "2", operationContent = "设置课程限制")
    public ResponseObject<Boolean> setCourseLimit(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                                  @ApiParam(value = "教学班 日期禁排实体类 ", required = true) @Validated @RequestBody CourseLimitSetVo vo) {
        ruleTimeProhibitService.setCourseLimit(taskId, vo);
        return ResponseObject.ofSuccess();
    }

    @GetMapping("/clear")
    @ApiOperation("重置设置")
    @LogAnnotation(operationType = "4", operationContent = "重置设置")
    public ResponseObject<Object> clear(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                        @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId) {
        ruleTimeProhibitService.clear(taskId, baseCollegeId);
        return ResponseObject.ofSuccess();
    }

    @GetMapping("/copy")
    @ApiOperation("复制设置")
    @LogAnnotation(operationType = "1", operationContent = "复制设置")
    public ResponseObject<Object> copy(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId,
                                       @ApiParam(value = "开课单位", required = true) @RequestParam String schoolId,
                                       @ApiParam(value = "开课单位", required = true) @RequestParam String term,
                                       @ApiParam(value = "开课单位", required = false) @RequestParam(required = false) String baseCollegeId) {
        ruleTimeProhibitService.copy(taskId, schoolId, term, baseCollegeId);
        return ResponseObject.ofSuccess();
    }

    @ApiOperation("同步更新")
    @PostMapping(value = "/synchronize")
    @LogAnnotation(operationType = "4", operationContent = "同步更新")
    public ResponseObject<Object> synchronizeUpdate(@ApiParam(value = "任务流程id", required = true) @RequestHeader Integer taskId) {
        ruleTimeProhibitService.synchronizeUpdate(taskId);
        return ResponseObject.ofSuccess();
    }


}
